//
//  FXCAouthViewController.m
//  XinLangFeiBo
//
//  Created by dllo on 16/2/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "FXCAouthViewController.h"
#import "FXCTabBarViewController.h"
#import "FXCNetWorkTool.h"
#import <AFNetworking.h>
#import "FXCAccount.h"



@interface FXCAouthViewController ()<UIWebViewDelegate>

@end

@implementation FXCAouthViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    NSLog(@"%@", FXCResquestTokeURLStr);
    UIWebView *webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    webView.delegate = self;
    [self.view addSubview:webView];
    NSURL *url = [NSURL URLWithString:FXCResquestTokeURLStr];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
//把请求转换成字符串
     NSString *urlStr = request.URL.absoluteString;
//    NSLog(@"%@", urlStr);
    
    
//    判断字符串是否含有code
    NSRange range = [urlStr rangeOfString:@"code="];
    
    if (range.length) {
//        截取code码
        NSString *code = [urlStr substringFromIndex:range.location + range.length];
        
        //        创建post参数字典
        
        NSDictionary *param = @{@"client_id":@"3411526181",
                                @"client_secret":@"4e8375c35c0cd264e4e88e7bfaf9cbde",
                                @"grant_type": @"authorization_code",
                                @"code":code,
                                @"redirect_uri":@"https://open.weibo.cn/oauth2/default.html"};
        
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil]];
        [manager POST:@"https://open.weibo.cn/oauth2/access_token" parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            //保存账号信息到沙盒
            FXCAccount *account = [FXCAccount mj_objectWithKeyValues:responseObject];
            
            [NSKeyedArchiver archiveRootObject:account toFile:FXCAccountFile];
            //            NSLog(@"%@",responseObject);
            FXCTabBarViewController *tabBar = [[FXCTabBarViewController alloc] init];
            [UIApplication sharedApplication].keyWindow.rootViewController = tabBar;
            
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"%@", error);
        }];
        
//  不进行授权页跳转
        return NO;
    }


    
    return YES;
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
